package com.itheima.aop;

import com.itheima.dao.AccountDao;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;

import javax.sql.DataSource;

public class TxAdvice {
    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
    public Object getTransfer(ProceedingJoinPoint pjp) throws Throwable {
        DataSourceTransactionManager manager = new DataSourceTransactionManager(dataSource);
        //底层开启事务
        TransactionStatus status = manager.getTransaction(null);
        Object proceed = pjp.proceed(pjp.getArgs());
        //提交事务
        manager.commit(status);
        return proceed;
    }
}
